/*
 * @(#)ButtonEditor.java Jan 19, 2012 - 7:23:27 PM
 * 
 * Copyright 2011 Cin/UFPE/VSPEED
 */
package br.ufpe.cin.vspeed.gui.render;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import speed2010.reformulacao.query.InstanceResult;
import br.ufpe.cin.vspeed.gui.GUIConstants;
import br.ufpe.cin.vspeed.gui.PanelResultInformation;

/**
 * @author <a href="mailto:andreza.lba@gmail.com">Andreza Leite de Alencar</a>
 */
public class ButtonEditor extends DefaultCellEditor {
	private static final long serialVersionUID = 2110874830097238826L;

	private final JButton button;
	private String label;
	private boolean isPushed;

	private String selectValue;
	private InstanceResult instanceResult;

	private final Map<Integer, InstanceResult> resultsMap;

	public ButtonEditor(final JCheckBox checkBox, final Map<Integer, InstanceResult> resultsMap) {
		super(checkBox);
		this.resultsMap = resultsMap;
		this.button = new JButton();
		this.button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				fireEditingStopped();
			}
		});
	}

	@Override
	public Component getTableCellEditorComponent(final JTable table, final Object value, final boolean isSelected,
			final int row, final int column) {
		if (isSelected) {
			this.button.setForeground(table.getSelectionForeground());
			this.button.setBackground(table.getSelectionBackground());
		} else {
			this.button.setForeground(table.getForeground());
			this.button.setBackground(table.getBackground());
		}
		this.label = (value == null) ? "" : value.toString();
		this.button.setText(this.label);
		this.isPushed = true;

		final String valueAt = (String) table.getValueAt(row, 1);
		if (valueAt != null) {
			this.selectValue = valueAt.split(":")[1];
			if (this.resultsMap != null) {
				final InstanceResult result = this.resultsMap.get(row);
				this.instanceResult = result;
			}
		}

		return this.button;
	}

	@Override
	public Object getCellEditorValue() {
		if (this.isPushed) {
			SwingUtilities.invokeLater(new Runnable() {

				@Override
				public void run() {
					final JFrame frameResultInfo = new JFrame();
					final PanelResultInformation panelResultInformation = new PanelResultInformation(
							ButtonEditor.this.instanceResult);
					frameResultInfo.getContentPane().add(panelResultInformation);
					frameResultInfo.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
					frameResultInfo.pack();
					frameResultInfo.setLocationRelativeTo(frameResultInfo.getRootPane());
					frameResultInfo.addWindowListener(new java.awt.event.WindowAdapter() {
						@Override
						public void windowOpened(final java.awt.event.WindowEvent evt) {
							panelResultInformation.initializeTopology(ButtonEditor.this.selectValue);
						}
						@Override
						public void windowClosed(final java.awt.event.WindowEvent evt) {
							panelResultInformation.formWindowClosed(evt);
						}
					});
					frameResultInfo.setVisible(true);
					GUIConstants.GUI_INSTANCE.setEnabled(false);
					//frameResultInfo.setAlwaysOnTop(true);
				}
			});
		}
		this.isPushed = false;

		return this.label;
	}

	@Override
	public boolean stopCellEditing() {
		this.isPushed = false;

		return super.stopCellEditing();
	}

	@Override
	protected void fireEditingStopped() {
		super.fireEditingStopped();
	}
}
